import Toast from '@vant/weapp/toast/toast';
const app = getApp()
const api = app.globalData.api
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fileList:[],//附件
    id:'',
    isBack:false,
    staff:[],//转交或增加的审批人
    info:{},
    auditText:'',
    active:0,
    steps: [],
  },
  /**编辑 */
  edit(e){
    wx.navigateTo({
      url: '../paymentEdit/paymentEdit?id='+this.data.id,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let id = options.id;
    if(api.isBlank(id)){
      wx.showToast({
        title: '未找到ID',
        icon: 'none',
      })
      setTimeout(function(){
        wx.navigateBack({
          delta: 1,
        })
      },3000)
   } else{
     this.requestData(id);
     api.setBackFlag();
   }
  },

  /**请求数据 */
  requestData(id){
    let that = this;
    api.curlPost('?cla=paymentApp&fun=detail',{id:id}).then((res)=>{
      that.setData({
        id:res.data.result.id,
        info:res.data.result,
        fileList:res.data.result.attachFile?res.data.result.attachFile:[],
        active:res.data.result.active,
        steps:res.data.result.auditRecord,
        isBack:false,
        auditText:'',
      })
    })
  },

  
/**提交审批 */
commitAudit(){
  let id = this.data.id;
  let that = this;
  api.curlPost('?cla=auditApp&fun=getPara',{id:id,flowName:'付款申请',act:'start'}).then((res)=>{
    api.curlPost('?cla=auditApp&fun=operate',{id:id,body:res.data.result,act:'start'}).then((back)=>{
      if(back.data.auditType =='审批人自选'){
        let type = 'addAuditor';
        wx.navigateTo({
          url: '../chooseStaff/chooseStaff?id='+id+'&type='+type+'&flowName=付款申请',
        })
      }else{
        that.requestData(id);
        Toast("提交成功")
      }
    })
  })
},
/**审批 */
audit(e){
  let that = this;
  let id = this.data.id;
  let type = e.currentTarget.dataset.type;
 
  api.curlPost('?cla=auditApp&fun=getPara',{id:id,flowName:'付款申请',act:'audit'}).then((res)=>{
    let para = {
      state:type,
      id:id,
      body:res.data.result,
      text:that.data.auditText
    }
    api.curlPost('?cla=auditApp&fun=auditLayerEdit',para).then((back)=>{
      that.requestData(id);
      Toast(back.warn)
    })
  })
},
/**撤销 */
cancel(){
  let id = this.data.id;
  let that = this;
  api.curlPost('?cla=auditApp&fun=getPara',{id:id,flowName:'付款申请',act:'cancel'}).then((res)=>{
    api.curlPost('?cla=auditApp&fun=operate',{id:id,body:res.data.result,act:'cancel'}).then((back)=>{
      
      that.requestData(id);
      Toast(back.warn)
    })
  })
},

/**增加审批人或转交给其他人审批 */
transfer(e){
  let id = this.data.id;
  let type = e.currentTarget.dataset.type;
  wx.navigateTo({
    url: '../chooseStaff/chooseStaff?id='+id+'&type='+type+'&flowName=付款申请',
  })

},

/**催办 */
press(){
  let id = this.data.id;
  let that = this;
  api.curlPost('?cla=auditApp&fun=operate',{id:id,act:'press'}).then((back)=>{
      
    that.requestData(id);
    Toast(back.warn)
  })

},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(this.data.isBack){
      this.requestData(this.data.id)
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})